package cn.zl.ZLM.event.callback.impl;

import cn.hutool.core.util.StrUtil;
import cn.zl.ZLM.core.ZLMApi;
import cn.zl.ZLM.event.callback.inter.IMKPublishCallBack;
import cn.zl.ZLM.helper.ZLMApiHelper;
import cn.zl.ZLM.structure.MK_MEDIA_INFO;
import cn.zl.ZLM.structure.MK_PUBLISH_AUTH_INVOKER;
import cn.zl.ZLM.structure.MK_SOCK_INFO;
import cn.yunbamboos.Const;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.zl.common.ZLConst;
import cn.zl.mapper.StreamPushMapper;
import cn.zl.model.StreamPush;
import com.sun.jna.CallbackThreadInitializer;
import com.sun.jna.Native;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class MKPublishCallBack implements IMKPublishCallBack {

    private static final Logger logger = LoggerFactory.getLogger(MKPublishCallBack.class);

    @Resource
    private StreamPushMapper streamPushMapper;

    public MKPublishCallBack() {
        //回调使用同一个线程
        Native.setCallbackThreadInitializer(this, new CallbackThreadInitializer(true, false, "MediaPublishThread"));
    }

    @Override
    public void invoke(MK_MEDIA_INFO url_info, MK_PUBLISH_AUTH_INVOKER invoker, MK_SOCK_INFO sender) {
        ZLMApi ZLM_API = ZLMApiHelper.getInstance().getApi();
        //通过ZLM_API.mk_media_info_get_*可以查询流的各种信息
        String schema = ZLM_API.mk_media_info_get_schema(url_info);
        String host = ZLM_API.mk_media_info_get_host(url_info);
        int port = ZLM_API.mk_media_info_get_port(url_info);
        String app = ZLM_API.mk_media_info_get_app(url_info);
        String stream = ZLM_API.mk_media_info_get_stream(url_info);
        //这里拿到访问路径后(例如rtmp://xxxx/xxx/xxx?token=xxxx其中?后面就是拿到的参数)的参数
        String params = ZLM_API.mk_media_info_get_params(url_info);
        String url = StrUtil.format("{}://{}:{}/{}/{}", schema, host, port, app, stream);
        if (StrUtil.isNotBlank(params)) {
            url = url + "?" + params;
        }
        // 国标推流
        if("rtp".equals(schema)){
            logger.info("[接收RTP推流] {}", url);
            ZLM_API.mk_publish_auth_invoker_do(invoker, "", 0, 0);
            return;
        }
        // 模拟视频流
        if(ZLConst.VIRTUAL_VIDEO.APP.equals(app)){
            ZLM_API.mk_publish_auth_invoker_do(invoker, "", 0, 0);
            return;
        }
        Optional<StreamPush> optional = streamPushMapper.queryForObject(Conditions.create("app", app).add("stream", stream));
        if (optional.isPresent()) {
            logger.info("[接收推流] {}", url);
            ZLM_API.mk_publish_auth_invoker_do(invoker, "", 0, 0);
        } else {
            logger.info("[接收推流] 未配置 {}", url);
            ZLM_API.mk_publish_auth_invoker_do(invoker, "未配置的推流信息", 0, 0);
        }
        // err_msg返回 空字符串表示鉴权成功 否则鉴权失败提示
        // MediaServerContext.ZLM_API.mk_publish_auth_invoker_do(invoker,"",0,0);
        // 推流回调参数 用来控制转协议、录制等操作
        // ZLMApiHelper.getInstance().getApi().mk_publish_auth_invoker_do(invoker, "", 0, 0);
    }
}
